#pragma once

#include <iostream>
#include <string>
#include <unistd.h>
#include <cerrno>
#include <cstring>
#include <strings.h>
#include<pthread.h>
#include <cstdlib>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h> //网络头文件
#include <netinet/in.h>
// 客户端
namespace Client
{
    using namespace std;
    class udpClient
    {
    public:
        udpClient(const string &serverip, const uint16_t &port)
            : _serverip(serverip), _serverport(port), _sockfd(-1), _quit(false)
        {
        }

        void initClient()
        {
            // 1. 创建socket(只需要创建套接字即可)
            _sockfd = socket(AF_INET, SOCK_DGRAM, 0);
            if (_sockfd == -1)
            {
                cerr << "socket error: " << errno << " : " << strerror(errno) << endl;
                exit(2);
            }
            cout << "socket success: " << " : " << _sockfd << endl;
            
            // 2. client必须bind, 但不需要程序员自己显示的bind!
            // 写服务器的是一家公司，写client的是无数家公司 -- 由OS自动形成端口进行bind！
            // OS在什么时候绑定？如何绑定？
        }
        
        static void* readMessage(void* args)
        {
            int sockfd = *(static_cast<int*>(args));
            pthread_detach(pthread_self());
            while(true)
            {
                char buffer[1024];
                struct sockaddr_in temp;
                socklen_t temp_len = sizeof(temp);
                size_t n = recvfrom(sockfd, buffer, sizeof(buffer)-1, 0, (struct sockaddr *)&temp, &temp_len);

                if(n >= 0) buffer[n] = 0;
                cout << buffer << endl;
            }
            return nullptr;
        }
        void run()
        {
            pthread_create(&_reader, nullptr, readMessage, (void*)&_sockfd);
            //pthread_create(&_writer, nullptr, recvMessage, nullptr);

            struct sockaddr_in server;
            memset(&server, 0, sizeof(server));
            server.sin_family = AF_INET;
            server.sin_addr.s_addr = inet_addr(_serverip.c_str());
            server.sin_port = htons(_serverport);


            string message;
            char cmdline[1024];
            while (!_quit)
            {
              
                // cin >> message;
                fprintf(stderr, "Enter# ");
                fflush(stderr);
                fgets(cmdline, sizeof(cmdline), stdin);
                cmdline[strlen(cmdline)-1] = 0;
                //sendto(_sockfd, message.c_str(), message.size(), 0, (struct sockaddr *)&server, sizeof(server));
                message = cmdline;
                sendto(_sockfd, message.c_str(), message.size(), 0, (struct sockaddr *)&server, sizeof(server));

                // //接收 --- 是给demo1用的，因为demo1中的server有sendto
                // char buffer[1024];
                // struct sockaddr_in temp;
                // socklen_t temp_len = sizeof(temp);
                // size_t n = recvfrom(_sockfd, buffer, sizeof(buffer)-1, 0, (struct sockaddr*)&temp, &temp_len);
                // if(n >= 0) buffer[n] = 0;
                // std::cout << buffer << std::endl;

            }


        }

        ~udpClient()
        {
        }

    private:
        int _sockfd;
        string _serverip;     // 目标ip
        uint16_t _serverport; // 目标端口号
        bool _quit;

        pthread_t _reader;
        //pthread_t _writer;
    };
}